﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FrameDisplay.Services;
using System.IO;
using System.Drawing.Imaging;
using System.Threading;

namespace FrameDisplayClient.UI.ScreenSender
{
    public partial class ScreenSenderControl : UserControl
    {       
        private IFrameDisplayService frameDisplayService;
        private string accessCode;   

        public ScreenSenderControl()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            Screen[] screens = Screen.AllScreens;
            foreach (Screen screen in screens)
            {
                this.comboBoxScreen.Items.Add(new ScreenItem(screen));
            }

            this.comboBoxScreen.SelectedIndex = 0;

            this.radioButtonScreen.Checked = true;

            this.windowDragSelectorControl1.WindowSelected += new EventHandler(windowDragSelectorControl1_WindowSelected);
        }        


        public void SetFrameDisplayService(IFrameDisplayService frameDisplayService)
        {
            this.frameDisplayService = frameDisplayService;
        }

        public void SetAccessCode(string accessCode)
        {
            this.accessCode = accessCode;
        }


        private void buttonBack_Click(object sender, EventArgs e)
        {
            if (this.Parent != null)
            {
                this.Parent.Controls.Remove(this);
                this.Dispose();
            }
        }

        private ScreenSenderWorker CreateScreenSenderWorker()
        {    
            if (this.radioButtonScreen.Checked)
            {
                ScreenItem screenItem = (ScreenItem)this.comboBoxScreen.SelectedItem;
                if (screenItem != null)
                {
                    return new DisplayScreenSenderWorker(this.frameDisplayService, this.accessCode, screenItem.Screen);                    
                }
            }
            else if (this.radioButtonWindow.Checked)
            {
                IntPtr window = this.windowDragSelectorControl1.SelectedWindow;
                if (window != IntPtr.Zero)
                {
                    return new WindowScreenSenderWorker(this.frameDisplayService, this.accessCode, window);
                }
            }

            return null;
        }
        

        private void buttonSendImage_Click(object sender, EventArgs e)        
        {
            ScreenSenderWorker screenSenderWorker = this.CreateScreenSenderWorker();
            if (screenSenderWorker != null)
            {
                screenSenderWorker.SendOnceAsync();
            }
        }       

        private void buttonStartContinuous_Click(object sender, EventArgs e)
        {
            ScreenSenderWorker screenSenderWorker = this.CreateScreenSenderWorker();
            screenSenderWorker.Start();

            StopScreenSenderControl stopScreenSenderControl = new StopScreenSenderControl();
            stopScreenSenderControl.SetScreenSenderWorker(screenSenderWorker);
            stopScreenSenderControl.Dock = DockStyle.Fill;            

            this.Controls.Add(stopScreenSenderControl);
            stopScreenSenderControl.BringToFront();
        }


        void windowDragSelectorControl1_WindowSelected(object sender, EventArgs e)
        {
            IntPtr window = this.windowDragSelectorControl1.SelectedWindow;
            if (window == IntPtr.Zero)
            {
                this.labelWindowName.Text = "Drag target over window to send.";
            }
            else
            {
                this.labelWindowName.Text = SharpScraps.Win32.USER32Helper.GetWindowText(window);
            }
        }


        private class ScreenItem
        {
            private Screen screen;

            public ScreenItem(Screen screen)
            {
                this.screen = screen;
            }

            public Screen Screen
            {
                get { return this.screen; }
            }

            public override string ToString()
            {
                return screen.DeviceName;
            }
        }
        
    }    
    
}
